//
//  ViewController.m
//  TestAnimation
//
//  Created by mokai on 14-8-27.
//  Copyright (c) 2014年 mokai. All rights reserved.
//

#import "TestAnimationsCtr.h"

@interface TestAnimationsCtr ()
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property(nonatomic,strong) UIImage *baidu;
@property(nonatomic,strong) UIImage *google;

- (IBAction)testCommitAnimations:(id)sender;
- (IBAction)test:(id)sender;
- (IBAction)test1:(id)sender;
- (IBAction)kCATransitionFade:(id)sender;
- (IBAction)kCATransitionPush:(id)sender;
@end

@implementation TestAnimationsCtr

-(void)viewDidLoad{
    [super viewDidLoad];
    _google = [UIImage imageNamed:@"google"];
}
/***
 UIView Animation
 setAnimationDuration 动画维持时长
 setAnimationRepeatAutoreverses 是否有反向动画
 
 
 setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache;

 设置预置的动画类型，transition可选值
     UIViewAnimationTransitionFlipFromLeft,
     UIViewAnimationTransitionFlipFromRight,
     UIViewAnimationTransitionCurlUp,
     UIViewAnimationTransitionCurlDown,
 
 ***/
- (IBAction)testEasyAnimation:(id)sender {
    [UIView beginAnimations:@"uiview anim" context:nil];
    [UIView setAnimationDuration:1.0f];//动画维持时长
    //[UIView setAnimationRepeatAutoreverses:YES];//保持动画后样子
    [UIView setAnimationRepeatCount:2];//设置动画重复次数
    
    CGRect rect =  _image.frame;
    rect.origin.y +=20;
    _image.frame = rect;
    
    [UIView commitAnimations];
}


- (IBAction)UIViewAnimationTransitionFlipFromRight:(id)sender {
    
    [UIView beginAnimations:@"uiview anim" context:nil];
    [UIView setAnimationDuration:1.0f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.image cache:YES];
    [UIView commitAnimations];
}

- (IBAction)UIViewAnimationTransitionCurlUp:(id)sender {
    [UIView beginAnimations:@"uiview anim" context:nil];
    [UIView setAnimationDuration:1.0f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.image cache:YES];
    [UIView commitAnimations];
}

- (IBAction)UIViewAnimationTransitionCurlDown:(id)sender {
    [UIView beginAnimations:@"uiview anim" context:nil];
    [UIView setAnimationDuration:1.0f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.image cache:YES];
    [UIView commitAnimations];
}


-(void)imageChange{
    NSData *data = UIImagePNGRepresentation(_image.image);
    if ([data isEqualToData:UIImagePNGRepresentation(_google)]) {
        _image.image = _baidu;
    }else{
        if (_baidu==nil) {
            _baidu = _image.image;
        }
        _image.image = _google;
    }
}

/**
 Core Animation
 
 
 1、使用：CATransition
 
 
 transition.type 的类型可以有
 淡化、推挤、揭开、覆盖四种效果
 NSString * const kCATransitionFade;
 NSString * const kCATransitionMoveIn;
 NSString * const kCATransitionPush;
 NSString * const kCATransitionReveal;
 这四种，
 
 
 私有的类型的动画类型：
 立方体、吸收、翻转、波纹、翻页、反翻页、镜头开、镜头关
 
 transition.subtype 为方向
 也有四种
 NSString * const kCATransitionFromRight;
 NSString * const kCATransitionFromLeft;
 NSString * const kCATransitionFromTop;
 NSString * const kCATransitionFromBottom;
 
 animation.type = @"cube"
 animation.type = @"suckEffect";
 animation.type = @"oglFlip";//不管subType is "fromLeft" or "fromRight",official只有一种效果
 animation.type = @"rippleEffect";
 animation.type = @"pageCurl";
 animation.type = @"pageUnCurl"
 animation.type = @"cameraIrisHollowOpen ";
 animation.type = @"cameraIrisHollowClose ";
 
 
 上面这些私有的动画效果，在实际应用中要谨慎使用。因为在app store审核时可能会以为这些动画效果而拒绝通过。
 **/
- (IBAction)kCATransitionFade:(id)sender {
    CATransition *transition = [CATransition animation];
    transition.duration = 1;
    transition.type = kCATransitionFade;
    transition.subtype = kCATransitionFromTop;
    [self imageChange];
    [self.image.layer addAnimation:transition forKey:@"CaTranstion"];
}

- (IBAction)kCATransitionPush:(id)sender {
    CATransition *transition = [CATransition animation];
    transition.duration = 1;
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromTop;
    //[self imageChange];
    [self.image.layer addAnimation:transition forKey:@"CaTranstion"];
}

- (IBAction)kCATransitionReveal:(id)sender {
    CATransition *transition = [CATransition animation];
    transition.duration = 1;
    transition.type = kCATransitionReveal;
    transition.subtype = kCATransitionFromTop;
    //[self imageChange];
    [self.image.layer addAnimation:transition forKey:@"CaTranstion"];
}

- (IBAction)kCATransitionMoveIn:(id)sender {
    CATransition *transition = [CATransition animation];
    transition.duration = 1;
    transition.type = kCATransitionMoveIn;
    transition.subtype = kCATransitionFromTop;
    //[self imageChange];
    [self.image.layer addAnimation:transition forKey:@"CaTranstion"];
}

/**
 2、CABasicAnimation
 **/
- (IBAction)opacity:(id)sender {
    self.view.layer.opacity = 1;  // 记得更新图层树
}

-(IBAction)CABasicAnimationOpacity:(UIButton *)sender{
    CABasicAnimation* fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fadeAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    fadeAnimation.toValue = [NSNumber numberWithFloat:0.5];
    fadeAnimation.duration = 1.0;
    
    [self.view.layer addAnimation:fadeAnimation forKey:nil];
    //改变图层实际的最后数据值
    self.view.layer.opacity = 0.5f;  // 记得更新图层树
}

//利用path
- (IBAction)CAKeyframeAnimationPosition:(UIButton *)sender {
    // 创建一个实现两个弧线的CGPath（一个跳弹路径）
    CGMutablePathRef thePath = CGPathCreateMutable();
    CGPathMoveToPoint(thePath,NULL,74.0,74.0);
    CGPathAddCurveToPoint(thePath,NULL,74.0,100.0,
                          320.0,200.0,
                          320.0,74.0);
    CGPathAddCurveToPoint(thePath,NULL,320.0,300.0,
                          266.0,100.0,
                          166.0,74.0);
    CAKeyframeAnimation * theAnimation;
    //创建一个动画对象，指定位置属性作为键路径
    theAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
    theAnimation.path=thePath;
    theAnimation.duration=5.0;
    CGPathRelease(thePath);
    // 为图层添加动画
    [sender.layer addAnimation:theAnimation forKey:nil];
}

//利用values
- (IBAction)rotate360:(id)sender {
    CAKeyframeAnimation *theAnimation = [CAKeyframeAnimation animation];
    
    //把值调换即可调整旋转方向
    theAnimation.values = [NSArray arrayWithObjects:
                           [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0, 0,0,1)],
                           [NSValue valueWithCATransform3D:CATransform3DMakeRotation(3.13, 0,0,1)],
                           [NSValue valueWithCATransform3D:CATransform3DMakeRotation(6.26, 0,0,1)],
                           nil];
   // theAnimation.cumulative = YES;
    theAnimation.duration = 3;
    theAnimation.repeatCount = 1;
   // theAnimation.removedOnCompletion = YES;
    
    theAnimation.timingFunctions = [NSArray arrayWithObjects:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],
                                    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],
                                    nil
                                    ];
    
    [_image.layer addAnimation:theAnimation forKey:@"transform"];
}


@end
